package com.ckm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckm.entity.Teacher;
import com.ckm.exception.BusinessException;
import com.ckm.mapper.TeacherMapper;
import com.ckm.service.TeacherService;
import com.ckm.util.FindObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public String login(Teacher teacher) {
        if (teacher.getId()==null&&teacher.getName()==null&&teacher.getPhone()==null){
            throw new BusinessException(250,"账号不能为空");
        }
        if (teacher.getPassword()==null){
            throw new BusinessException(250,"密码不能为空");
        }
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        if (teacher.getId()!=null){
            wrapper.eq("id", teacher.getId());
        }else if (teacher.getName()!=null){
            wrapper.eq("name", teacher.getName());
        }else {
            wrapper.eq("phone", teacher.getPhone());
        }
        Teacher loginTeacher = teacherMapper.selectOne(wrapper);
        if (loginTeacher == null){
            throw new BusinessException(250,"账号或密码错误");
        }
        if (!loginTeacher.getPassword().equals(teacher.getPassword())){
            throw new BusinessException(250,"密码错误");
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set(token,loginTeacher, 30, TimeUnit.MINUTES);
        return token;
    }

    @Override
    public Teacher getTeacher(String token) {
        if (Boolean.FALSE.equals(redisTemplate.hasKey(token))){
            throw new BusinessException(250,"无此信息");
        }
        return (Teacher) redisTemplate.opsForValue().get(token);
    }

    @Override
    public void addTeacher(List<Teacher> teacherList) {
        if (teacherList==null||teacherList.isEmpty()){
            throw new BusinessException(250,"添加数据集为空");
        }
        for (Teacher teacher : teacherList) {
            teacherMapper.insert(teacher);
        }
        log.info("添加数据：{}", teacherList);
    }

    @Override
    public void deleteTeacher(Integer id) {
        if (id==null){
            throw new BusinessException(250,"删除参数为空");
        }
        if (teacherMapper.selectById(id)==null){
            throw new BusinessException(250,"无此用户");
        }
        teacherMapper.deleteById(id);
    }

    @Override
    public void updateTeacher(Teacher teacher) {
        if (teacher.getId()==null){
            throw new BusinessException(250,"更新老师信息缺少id");
        }
        Teacher select = teacherMapper.selectById(teacher.getId());
        if (select==null){
            throw new BusinessException(250,"无此用户");
        }
        if (select.equals(teacher)){
            throw new BusinessException(250,"信息无更改");
        }
        teacherMapper.updateById(teacher);
    }

    @Override
    public PageInfo<Teacher> getTeacherPage(Teacher teacher,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(findTeacher(teacher));
    }

    @Override
    public List<Teacher> findTeacher(Teacher teacher) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        FindObjectUtil.fullQueryWrapper(teacher,wrapper);
        return teacherMapper.selectList(wrapper);
    }
}
